<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ attribute name="id" type="java.lang.String" required="true" description="编号"%>

<%@ attribute name="fieldName" type="java.lang.String" required="true" description="字段名称"%>
<%@ attribute name="fieldId" type="java.lang.String" required="false" description="字段值"%>

<%@ attribute name="fileTypeDesc" type="java.lang.String" required="true" description="文件类型描述"%>
<%@ attribute name="fileTypeExts" type="java.lang.String" required="true" description="允许的文件类型"%>

<%@ attribute name="multi" type="java.lang.Boolean" required="false" description="是否支持上传多个文件"%>
<%@ attribute name="readonly" type="java.lang.Boolean" required="false" description="是否只读"%>
<%@ attribute name="required" type="java.lang.Boolean" required="false" description="是否必须"%>
<div id="upload_div">
    <input id="${id}_id" fieldName="${fieldName}_name" fieldId="${fieldId}_fieldId" fileTypeDesc="${fileTypeDesc }" fileTypeExts="${fileTypeExts }" readonly="${readonly }" required="${required}" multi="${multi }" type="hidden"/>

    <!-- 1、主表信息 -->
    <input type="hidden" name="${fieldName}" value="">

    <!-- 2、附件表信息 -->
    <input type="text" id="_${fieldName}Oname" name="accMap['${fieldName}'].originalName" class="easyui-validatebox" readonly="readonly" data-options="required:true,editable:false"><!-- 2.1、 附件文件名 -->
    <input type="hidden" id="_${fieldName}Path" name="accMap['${fieldName}'].path"><!-- 2.2、附件路径 -->
    <input type="hidden" id="_${fieldName}Isnew" name="accMap['${fieldName}'].isNew" value="false"><!-- 2.3、附件是否是新的 -->

    <!-- 3、操作 -->
    <a href="javascript:void(0)"
       onclick="
            document.getElementById('_${fieldName}Uploadcontainer').style.display=='none'?
            document.getElementById('_${fieldName}Uploadcontainer').style.display='block':
            document.getElementById('_${fieldName}Uploadcontainer').style.display='none';
           ">上传</a>
    <a href="javascript:void(0)" onclick="_imageBrowse('_${fieldName}Path')">预览</a>

    <!-- 4、上传组件 -->
    <div style="margin:10px 0;display:none;border:1px solid #c9e6f1;" id="_${fieldName}Uploadcontainer">
        <input type="file" id="_${fieldName}Fileupload"><!-- uploadify 需要 -->
    </div>

    <!-- 5、辅助js -->
    <script type="text/javascript">
        j(document).ready(function () {
            //5.1、加载上次成功上传的附件
            var fieldId = "${fieldId}";
            if (fieldId) {
                j.ajax({
                    type: 'POST',
                    contentType: 'application/x-www-form-urlencoded; charset=utf-8',
                    url: 'accessory/detail/' + fieldId,
                    success: function (data, textStatus) {
                        j("#_${fieldName}Oname").attr("value", data);
                        j("#_${fieldName}Path").attr("value", data.path);
                        j("#_${fieldName}Isnew").attr("value", false);
                        j('#_${fieldName}Oname').validatebox('validate');
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus + ';' + errorThrown);
                    }
                });
            }
            //5.2、uploadify 支持
            var queueId = "${fieldName}Queueid";
            j("#_${fieldName}Uploadcontainer").append(_initFileQueue(queueId));
            j("#_${fieldName}Fileupload").uploadify({
                queueID: queueId,//显示上传文件队列的元素id，
                fileSizeLimit: 0,//文件的极限大小，以字节为单位，0为不限制。1MB:1*1024*1024
                fileTypeDesc: '${fileTypeDesc }',//允许上传的文件类型的描述，在弹出的文件选择框里会显示
                fileTypeExts: '${fileTypeExts }',//允许上传的文件类型，限制弹出文件选择框里能选择的文件
                multi:'${multi }'
,                onUploadSuccess: function (file, data, response) {//每个文件上传成功时触发
                    data = eval('(' + data + ')');
                    var path = data.data;
                    j('input[id=\'_${fieldName}Oname\']').attr('value', file.name);
                    j('input[id=\'_${fieldName}Path\']').attr('value', path);
                    j("#_${fieldName}Isnew").attr("value", true);
                    j('input[id=\'_${fieldName}Oname\']').validatebox('validate');
                    document.getElementById('_${fieldName}Uploadcontainer').style.display = 'none';
                }
            });
        });
    </script>
</div>
</div>